<?php

namespace App\Http\Controllers\User;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\LeaseDaily;
use App\Models\User\BlackList;
use Illuminate\Http\Request;
use Modules\Car\Entities\Customer;

// 黑名单操作
class BlackListController extends Controller
{
    use ResponseData;
    public function list(BlackList $model,Request $request){
        $ispage = $request->input('ispage', '');
        $search = $request->input('search', '');
        $size = $request->input('size',10);
        $model = $model->whereNull('deleted_at');
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        }
        // $name = substr($search,-6,6);
        if($request->filled('ispage')){
            if ($request->filled('department')) {
                // $data = $model->where('department_id',$request->department)->where(function($query)use($request){
                $data = $model->where(function($query)use($request){
                    $search = $request->input('search', '');
                    return $query->where('name','like','%'.$search.'%')->orWhere('phone','like','%'.$search.'%')->orWhere('id_number',$search);
                    // return $query->where('name','like','%'.$search.'%')->orWhere('phone','like','%'.$search.'%')->orWhere('id_number','like','%'.$search.'%');
                })->orderByDesc('id')->paginate($size);
            } else {
                $data = $model->where(function($query)use($search){
                    return $query->where('name','like','%'.$search.'%')->orWhere('phone','like','%'.$search.'%')->orWhere('id_number',$search);
                })->orderByDesc('id')->paginate($size);
                // $data = $model->where('name','like','%'.$search.'%')->orWhere('phone','like','%'.$search.'%')->orWhere('id_number','like','%'.$search.'%')->orderByDesc('id')->paginate($size);
            }
        }elseif($request->filled('search')){
            $data = $model->where('phone', $search)->orWhere('id_number', $search)->first();
            // $data = $model->where('phone', 'REGEXP',$name.'$')->orWhere('id_number', 'REGEXP',$name.'$')->first();
        }else{
           $data = $model->newQuery()->orderByDesc('id')->paginate($size);
        }
        return $this->success($data);
    }
    public function store(BlackList $model,Request $request){
        $data = $request->all();
        $data = array_filter($data,function($item){
            return  !is_null($item);
        });
        $model->fill($data);
        $model->save();
        if($request->filled('customer_id')){
            Customer::query()->where('id',$request->customer_id)->update(['is_black'=>1]);
        }else{
            $data['is_black'] = 1;
            Customer::query()->create($data);
        }

        return $this->success('保存成功');
    }
    public function edit(BlackList $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function update(BlackList $model,Request $request){
        $data = $model->find($request->id);
        $map = $request->except('is_black');
        $map = array_filter($map,function($item){
            return  !is_null($item);
        });
        $data->update($map);
        if($request->filled('customer_id')){
            Customer::query()->where('id',$request->customer_id)->update(['is_black'=>$request->is_black]);
        }
        return $this->success('修改成功');
    }
    public function destory(BlackList $model,Request $request){
        $data = $model->find($request->id);
        $data->deleted_at = now();
        $data->save();
        return $this->success('删除成功');
    }
}
